/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmBao.java
 *
 * Created on May 21, 2011, 10:43:49 PM
 */

package Presentation;

import RSCall.BaoRSc;
import Entities.BAO;
import java.util.ArrayList;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author VooKa
 */
public class frmBao extends javax.swing.JInternalFrame {

    /** Creates new form frmBao */
    public frmBao() {
        initComponents();
        Creation();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        tbxTenBao = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        cbxDay = new javax.swing.JComboBox();
        btnAdd = new javax.swing.JButton();
        btnUpdate = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblLoaiBao = new javax.swing.JTable();

        setTitle(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("titlefrmBao"));
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameOpened(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel1.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N
        jLabel1.setText(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("newspaper name"));

        jLabel2.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N
        jLabel2.setText(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("publish date"));

        cbxDay.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Chủ nhật", "Thứ 2", "Thứ 3", "Thứ 4", "Thứ 5", "Thứ 6", "Thứ 7" }));

        btnAdd.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N
        btnAdd.setText(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("insert"));
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });

        btnUpdate.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N
        btnUpdate.setText(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("update"));
        btnUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUpdateActionPerformed(evt);
            }
        });

        btnDelete.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N
        btnDelete.setText(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("delete"));
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });

        tblLoaiBao.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tblLoaiBao.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblLoaiBaoMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblLoaiBao);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 550, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                            .addComponent(btnAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(32, 32, 32)
                            .addComponent(btnUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(32, 32, 32)
                            .addComponent(btnDelete, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                            .addComponent(jLabel2)
                            .addGap(44, 44, 44)
                            .addComponent(cbxDay, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                            .addComponent(jLabel1)
                            .addGap(44, 44, 44)
                            .addComponent(tbxTenBao))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(tbxTenBao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(cbxDay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAdd)
                    .addComponent(btnUpdate)
                    .addComponent(btnDelete))
                .addGap(30, 30, 30)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 176, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formInternalFrameOpened(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameOpened
        this.LoadData();
    }//GEN-LAST:event_formInternalFrameOpened

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        String name = tbxTenBao.getText().trim();
        if(name.isEmpty())
            JOptionPane.showMessageDialog(this,
                    java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("not enter newspaper name"));
        else{
            try{
                dbLoaiBao.Insert(name, cbxDay.getSelectedIndex()+1);
                this.LoadData();
                tbxTenBao.setText("");
                JOptionPane.showMessageDialog(this,
                        java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("added a newspaper"));
            }catch(Exception e){
                Logger.getLogger(frmBao.class.getName()).log(Level.SEVERE, null, e);
                JOptionPane.showMessageDialog(this, e.toString());
            }
        }
    }//GEN-LAST:event_btnAddActionPerformed

    private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUpdateActionPerformed
        if(this.tblLoaiBao.getSelectedRow() == -1){
            JOptionPane.showMessageDialog(this
                    , java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("must choice a newspaper to update"));
        }else{
            String name = tbxTenBao.getText().trim();
            if(name.isEmpty()){
                JOptionPane.showMessageDialog(this,
                        java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("not enter newspaper name"));
            }else{
                try{
                    dbLoaiBao.Update((String)this.tblLoaiBao.getValueAt(this.tblLoaiBao.getSelectedRow(), 1),
                            name, cbxDay.getSelectedIndex()+1);
                    this.LoadData();
                    tbxTenBao.setText("");
                    JOptionPane.showMessageDialog(this,
                            java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("updated a newspaper"));
                }catch(Exception e){
                    JOptionPane.showMessageDialog(this, e.toString());
                }
            }
        }
    }//GEN-LAST:event_btnUpdateActionPerformed

    private void tblLoaiBaoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblLoaiBaoMouseClicked
        try{
            int n = this.tblLoaiBao.getSelectedRow();
            tbxTenBao.setText((String)this.tblLoaiBao.getValueAt(n, 2));
            cbxDay.setSelectedItem((String)this.tblLoaiBao.getValueAt(n, 3));
        }catch(Exception e){
            JOptionPane.showMessageDialog(this, e.toString());
        }
    }//GEN-LAST:event_tblLoaiBaoMouseClicked

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
        if(this.tblLoaiBao.getSelectedRow() ==-1 ){
            JOptionPane.showMessageDialog(this,
                    java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("must choice a newspaper to delete"));
        }else{
            int n = JOptionPane.showConfirmDialog(this,
                    java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("really delete"),
                    java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("warn"),JOptionPane.YES_NO_OPTION);
            if(n == 0){
                try{
                    dbLoaiBao.Delete((String)this.tblLoaiBao.getValueAt(this.tblLoaiBao.getSelectedRow(), 1));
                    this.LoadData();
                    tbxTenBao.setText("");
                    JOptionPane.showMessageDialog(this,
                            java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("deleted a newspaper"));
                }catch(Exception e){
                    JOptionPane.showMessageDialog(this,e.toString());
                }
            }
        }
    }//GEN-LAST:event_btnDeleteActionPerformed
    private void LoadData(){
        try{
            ArrayList<BAO> lst = dbLoaiBao.GetAllRows();
            DefaultTableModel dtm = new DefaultTableModel(){
                @Override
                public boolean isCellEditable(int x,int y){
                    return false;
                }
            };
            dtm.addColumn(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("STT"));
            dtm.addColumn(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("mabao"));
            dtm.addColumn(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("newspaper name"));
            dtm.addColumn(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("publish date"));
            for(int i=0;i<lst.size();i++)
            {
                Vector _v = new Vector();
                BAO _b = lst.get(i);
                _v.add(i+1);
                _v.add(_b.getMaBao());
                _v.add(_b.getTenBao());
                _v.add(Utilities.Utilities.GetThu(_b.getThuPhatHanh()));
                dtm.addRow(_v);
            }
            this.tblLoaiBao.setModel(dtm);
            this.tblLoaiBao.getColumnModel().getColumn(0).setPreferredWidth(10);
            this.tblLoaiBao.getColumnModel().getColumn(1).setPreferredWidth(30);
        }
        catch(Exception e)
        {
            JOptionPane.showMessageDialog(this, e.toString());
            Logger.getLogger(frmBao.class.getName()).log(Level.SEVERE, null, e);
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnUpdate;
    private javax.swing.JComboBox cbxDay;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblLoaiBao;
    private javax.swing.JTextField tbxTenBao;
    // End of variables declaration//GEN-END:variables
    private RSCall.BaoRSc dbLoaiBao;
    private void Creation(){
        try {
            dbLoaiBao = new BaoRSc();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(rootPane, ex.toString());
            Logger.getLogger(frmBao.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}